package cn.yudao.system.controller;

import cn.yudao.basic.annotation.PreAuthorize;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import cn.yudao.system.domain.SystemType;
import cn.yudao.system.query.SystemTypeQuery;
import cn.yudao.system.service.ISystemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class SystemTypeController {

    @Autowired
    private ISystemTypeService service;



    @GetMapping
    @PreAuthorize(name = "查询所有数据字典类型",sn = "SystemType:list")
    public List<SystemType> loadAll(){
        List<SystemType> systemTypes = service.loadAll();
        return systemTypes;
    }

    @PostMapping
    @PreAuthorize(name = "查询数据字典类型",sn = "SystemType:query")
    public PageList<SystemType> queryType(@RequestBody SystemTypeQuery systemTypeQuery){
        PageList<SystemType> query = service.query(systemTypeQuery);
        System.out.println("分页" + query.getTotal());
        return query;
    }

    @PutMapping
    @PreAuthorize(name = "新增/修改数据字典类型",sn = "SystemType:addOrupt")
    public AjaxResult addOrupt(@RequestBody SystemType systemType){
        try {
            if (systemType.getId() == null){
                service.save(systemType);
            }else {
                service.upadte(systemType);
            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除数据字典类型",sn = "SystemType:delete")
    public AjaxResult remove(@PathVariable Long id){
        try {
            service.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PatchMapping
    public AjaxResult patchRom(@RequestBody Long[] ids){
        try {
            service.patchRom(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

}
